package com.eight.homepage.service.impl;


import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eight.common.pojo.TbLink;
import com.eight.homepage.dao.LinkDao;

import com.eight.homepage.service.LinkService;
import com.eight.homepage.vo.LinkVo;
import com.eight.homepage.vo.LinkVo2;
import com.eight.homepage.vo.LinkVo3;
import com.eight.homepage.vo.LinkVo4;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * @program: educaion-parent
 * @description:
 * @author: 平家祥
 * @create: 2023-05-27 14:48
 **/
@Service
public class LinkServiceImpl implements LinkService {

    @Autowired
   private LinkDao linkDao;
    @Override
    public Page<TbLink> getAllLink(Integer current, Integer pageSize, LinkVo linkVo) {
        Page<TbLink> page = new Page<>(current, pageSize);
        QueryWrapper<TbLink> wrapper = new QueryWrapper<>();
        if(StringUtils.hasText(linkVo.getLinkName())){
            wrapper.like("link_name",linkVo.getLinkName());
        }
        if(linkVo.getIsDisable()!=null){
            wrapper.eq("is_disable",linkVo.getIsDisable());
        }
        linkDao.selectPage(page,wrapper);
        return page;
    }

    @Override
    public Integer updateById(String linkId, Integer isDisable) {
        LinkVo2 linkVo2 = new LinkVo2();
        linkVo2.setLinkId(linkId);
        linkVo2.setIsDisable(isDisable);
        return linkDao.updateisDisable(linkVo2);
    }

    @Override
    public Integer updateById1(LinkVo3 linkVo3) {
        return linkDao.updateById1(linkVo3);
    }

    @Override
    public Integer insert(LinkVo4 linkVo4) {
        TbLink tbLink = new TbLink();
        tbLink.setLinkId(IdUtil.getSnowflake().nextId()+"");
        tbLink.setLinkUrl(linkVo4.getLinkUrl());
        tbLink.setLinkName(linkVo4.getLinkName());
        tbLink.setIsDisable(0);
        tbLink.setIsDeleted(0);
        tbLink.setGmtCreate(LocalDateTime.now());
        tbLink.setGmtModified(LocalDateTime.now());
        return linkDao.insert(tbLink);
    }

    @Override
    public Integer deleteById(String linkId) {
        return linkDao.selectById1(linkId);
    }
}
